//
//  YPPayBackItemView.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/3/1.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPPayBackItemView.h"
#import "NewPayBackTableViewCell.h"

@implementation YPPayBackItemView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    
    self = [super initWithFrame:frame style:style];
    if(self){
        
        [self setUp];
    }
    return self;
}

#pragma mark -- privateMethod
- (void)setUp{
    
    self.separatorStyle = 0;
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];
    self.allowsSelection = NO;
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, 10)];
    line.backgroundColor = UIColorFromRGB(0xf5f5f5);
    self.tableHeaderView = line;
    
    WeakSelf
    [self addGifHeaderWithRefreshingBlock:^{
        weakSelf.configModel.page = 1;
        [weakSelf setUpData];
    }];
    
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.configModel.page ++;
        [weakSelf setUpData];
    }];
    
    self.footer.hidden = YES;
    
}

- (void)setUpData{
    NSLog(@"%@,%ld",self.configModel.title,(long)self.configModel.page);
    NSDictionary *params = @{@"token":TOKEN,
                             @"page":@(self.configModel.page),
                             @"option":@"2",
                             @"type":[self.configModel.title isEqualToString:@"还款中"]?@"0":@"1"
                             };
    [YPHttpRequestMethod sendAsyRequestURL:CMD_PAYBACKDETAIL withData:params
                       withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        [self.header endRefreshing];
        [self.footer endRefreshing];
        if ([responseDictionary[@"status"] integerValue] == 0) {
            
            [self configModelsWithDataArray:responseDictionary[@"list"]];
            
            [self reloadData];
        }
        
    } withFailedBlock:^(NSError *error) {
        
        [self.header endRefreshing];
        [self.footer endRefreshing];
        
    }withIndicator:NO  atView:self];
}

- (void)configModelsWithDataArray:(NSArray *)dataArray{
    
    if (self.configModel.page == 1) {
        [self.configModel.dataArray removeAllObjects];
    }
    
    for (NSDictionary *dic in dataArray) {
        
        [self.configModel.dataArray addObject:[[MyReturnDetail alloc]initWithDic:dic]];
    }
    
    if ([dataArray count] < 5) {
        [self.footer noticeNoMoreData];
    }
    
    self.footer.hidden = self.configModel.dataArray.count==0?YES:NO;
    if (self.footer.hidden) {
        self.tableFooterView = [SelectView footViewWithheight:self.height];
    }else{
        self.tableFooterView = [UIView new];
    }
    
}

#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.configModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewPayBackTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:self.configModel.title];
    
    if (!cell) {
        cell = [[NewPayBackTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.configModel.title];
        WeakSelf
        cell.goDetailVC = ^(MyReturnDetail *model){
            if (weakSelf.goDetailBlock) {
                weakSelf.goDetailBlock(model);
            }
        };
    }
    
    cell.detailModel = self.configModel.dataArray[indexPath.row];

    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.width, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    
    
    NSArray *titleArray = @[@"标ID",[self.configModel.title isEqualToString:@"还款中"]?@"待还金额(元)":@"已还金额(元)",@"期数",@"时间"];
    [titleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UILabel *titleLabel = [UILabel creatWithFrame:CGRectMake(view.width/titleArray.count*idx, 0, view.width/titleArray.count, 30)
                                                 font:[UIFont systemFontOfSize:13]
                                                 text:obj
                                            textColor:kColor_greyAshTitleColor
                                        textAlignment:1];
        [view addSubview:titleLabel];
    }];
    
    [view addLine];
    
    return view;
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 30;
}

@end
